<template>
	<view class="u-wrap">
		<u-swiper :list="list" mode="none"></u-swiper>
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view v-for="(item,index) in tabbar" :key="index" class="u-tab-item" :class="[current==index ? 'u-tab-item-active' : '']"
				 :data-current="index" @tap.stop="swichMenu(index)" v-if="item.foods.length > 0">
					<text class="u-line-1">{{item.name}}</text>
					<uni-badge class="uni-badge-left-margin" :text="item.count" size="small" type="error" />
				</view>
			</scroll-view>
			<block v-for="(item,index) in tabbar" :key="index">
				<scroll-view scroll-y class="right-box" v-show="current==index">
					<view class="page-view">
						<view class="class-item">
							<view class="item-title">
								<text>{{item.name}}</text>
							</view>
							<view class="item-container">
								<view class="thumb-box" v-for="(item1, index1) in item.foods" :key="index1" v-if="item.loading">
									<view style="width: 100%;display: flex;">
										<view class="card-left">
											<u-image :src="imgUrl + item1.picture" mode="widthFix" @click="previewImg(imgUrl + item1.picture)" />
										</view>
										<view class="card-right" style="position: relative;">
											<view class="title">{{item1.name}}</view>
											<view class="description">{{item1.description || ''}}
											</view>
											<view style="position: absolute;right: 0;bottom: 0;">
												<u-number-box :min="0" :max="100" v-if="item1.oftenMeal" @plus="numPlus" @minus="numMinus" :index="index"
												 v-model="item1.num" style="float: right;" />
												<view v-else>
													<u-tag shape="circle" text="暂无食材" mode="plain" type="warning" />
												</view>
											</view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</block>
		</view>
		<view class="navigation" style="z-index: 999;">
			<view class="left">
				<view class="item car" @click="redirctToList">
					<u-icon name="order" :size="40"></u-icon>
					<view class="text u-line-1">历史订单</view>
				</view>
				<view class="item car" @click="orderPopup">
					<u-badge class="car-num" :count="count" type="error" :offset="[-3, -6]"></u-badge>
					<u-icon name="list-dot" :size="40" :color="$u.color['contentColor']"></u-icon>
					<view class="text u-line-1">已选菜品</view>
				</view>
				<!-- 	<view class="item car" @click="this.show = !this.show">
					<u-icon name="list-dot" :size="40" :color="$u.color['contentColor']"></u-icon>
					<view class="text u-line-1">订单信息</view>
				</view> -->
			</view>
			<view class="right">
				<u-button type="warning" shape="square" :disabled="count == 0" @click="submit">选好了</u-button>
			</view>
		</view>
		<u-popup v-model="orderShow" height="70%" mode="bottom" border-radius="20" z-index="800">
			<scroll-view scroll-y="true" style="height: 85%;">
				<view style="padding: 5%;height: 100%;">
					<view v-for="(item,index) in tabbar" :key="index" v-show="item.count > 0">
						<view style="width: 100%;display: flex;position: relative;" v-for="(item1,index1) in item.foods" :key="index1"
						 v-if="item1.num > 0">
							<view class="card-left" style="width: 30%;margin-left: 2%;margin-right: 8%;">
								<u-image :src="imgUrl+item1.picture" mode="widthFix" :lazy-load="true"></u-image>
							</view>
							<view class="card-right" style="width: 60%;">
								<view class="title">{{item1.name}}</view>
								<!-- <view class="description">{{item1.description}}</view> -->
								<view class="absolute">
									<u-number-box :min="0" @plus="numPlus" @minus="numMinus" :index="index" v-model="item1.num" style="float: right;" />
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</u-popup>

		<u-toast ref="uToast" />

	</view>
</template>

<script>
	import uniBadge from '@/compoents/uni-badge.vue';
	import {
		categoryQueryAll,
		flavorQueryAll,
		placeQueryAll
	} from '@/api/common.js'

	export default {
		data() {
			return {
				imgUrl: '',
				isLogin: false,
				tabbar: null,
				count: 0,
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				orderList: [],
				list: [],
				orderDetail: null,
				orderShow:false,
				addMeal:false
			}
		},
		components: {
			uniBadge,
		},
		onLoad(e) {
			if(e === null){
				return
			}
			
			if(e.addMeal === 'true'){
				this.addMeal = true
			}else{
				this.orderDetail = JSON.parse((decodeURIComponent(e.param)))
			}
		},
		created() {

			this.imgUrl = getApp().globalData.baseImgurl

			let that = this

			this.list.push(`${this.imgUrl}/banner.jpg`)


			categoryQueryAll().then(res => {
				that.tabbar = res.data
				that.tabbar[0].loading = true

			}).catch(res => {
				console.log(res)
				that.$refs.uToast.show({
					title: '加载失败',
					type: 'error'
				})
			})


		},

		methods: {
			orderPopup(){
				if(this.count > 0){
					this.orderShow = !this.orderShow
				}else{
					this.$refs.uToast.show({
						title: '购物车为空',
						type: 'warning',
					})
				}
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				if (index == this.current) return;
				this.current = index;
				// 如果为0，意味着尚未初始化

				this.tabbar[index].loading = true


				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;

			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			},
			numPlus(value) {
				this.tabbar[value.index].count++
				this.count++
			},
			numMinus(value) {
				this.tabbar[value.index].count--
				this.count--
			},
			redirctToList() {
				uni.navigateTo({
					url: "../orderHistory/orderHistory"
				})

			},
			submit() {

				let array = []
				let that = this
				this.tabbar.forEach(item => {
					if (item.count > 0) {
						item.foods.forEach(item1 => {
							if (item1.num > 0) {
								array.push(item1)
							}
						})
					}
				})
				
				if(this.addMeal){
					
					let addMealOrderDeatil =  getApp().globalData.addMealOrderDeatil;
					
					getApp().globalData.orderDetail = {
						id:addMealOrderDeatil.id,
						date: addMealOrderDeatil.mealTime,
						timestamp: addMealOrderDeatil.mealTime,
						num: addMealOrderDeatil.num,
						place: addMealOrderDeatil.place.id,
						placeName: addMealOrderDeatil.place.place,
						flavor: addMealOrderDeatil.flavor.id,
						flavorName: addMealOrderDeatil.flavor.flavor,
						foodsNum: that.count,
						remark: addMealOrderDeatil.remark,
						orderList: array
					}
					uni.navigateTo({
						url: '../orderCheck/orderCheck?addMeal=1'
					});
					return
				}
				
				let time = that.orderDetail.time
				getApp().globalData.orderDetail = {
					date: that.orderDetail.date,
					timestamp: time,
					num: that.orderDetail.num,
					place: that.orderDetail.place,
					placeName: that.orderDetail.placeName,
					flavor: that.orderDetail.flavor,
					flavorName: that.orderDetail.flavorName,
					foodsNum: that.count,
					remark: that.orderDetail.remark,
					orderList: array
				}

				uni.navigateTo({
					url: '../orderCheck/orderCheck?addMeal=0'
				});

			},
			previewImg(e) {
				uni.previewImage({
					urls: [e],
					indicator: 'none'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}


	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.u-search-inner {
		background-color: rgb(234, 234, 234);
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}

	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}

	.u-tab-view {
		width: 200rpx;
		height: 100%;
	}

	.u-tab-item {
		height: 110rpx;
		background: #f6f6f6;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}

	.u-tab-item-active {
		// position: relative;
		color: #000;
		font-size: 30rpx;
		font-weight: 600;
		background: #fff;
		border-left: 4px solid $u-type-primary;
	}



	.u-tab-view {
		height: 100%;
	}

	.right-box {
		background-color: rgb(250, 250, 250);
	}

	.page-view {
		padding: 16rpx;
	}

	.class-item {
		margin-bottom: 30rpx;
		background-color: #fff;
		padding: 16rpx;
		border-radius: 8rpx;
	}

	.item-title {
		font-size: 26rpx;
		color: $u-main-color;
		font-weight: bold;
	}

	.item-menu-name {
		font-weight: normal;
		font-size: 24rpx;
		color: $u-main-color;
	}

	.item-container {
		display: flex;
		flex-wrap: wrap;
	}

	.thumb-box {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin-top: 20rpx;
	}

	.item-menu-image {
		width: 120rpx;
		height: 120rpx;
	}

	.display-flex {
		margin-top: 10rpx;
		display: flex;
	}

	.card-left {
		width: 33.33%;
		height: 170rpx;
	}

	.card-right {
		margin-left: 3%;
		width: 63.66%;
	}

	.title {
		height: 30%;
		font-weight: bold;
		line-height: 100%;
	}

	.description {
		display: -webkit-box !important;
		overflow: hidden !important;
		text-overflow: ellipsis !important;
		word-break: break-all !important;
		-webkit-box-orient: vertical !important;
		-webkit-line-clamp: 2 !important;

	}

	.navigation {
		display: flex;
		border: solid 2rpx #f2f2f2;
		background-color: #ffffff;
		padding: 16rpx 0;

		.left {
			width: 60%;
			display: flex;
			// padding-left: 10%;
			// padding-right: 10%;
			font-size: 20rpx;

			.item {
				margin: 0 30rpx;

				&.car {
					text-align: center;
					position: relative;

					.car-num {
						position: absolute;
						top: -10rpx;
						right: -10rpx;
					}
				}
			}
		}

		.right {
			width: 40%;
			display: flex;
			font-size: 28rpx;
			padding-left: 10%;
			padding-right: 10%;
			align-items: center;

			.btn {
				line-height: 66rpx;
				padding: 0 30rpx;
				border-radius: 36rpx;
				color: #ffffff;
			}

			.cart {
				background-color: #ed3f14;
				margin-right: 30rpx;
			}

			.buy {
				width: 100%;
				background-color: #ff7900;
			}
		}
	}
	.absolute{
		position: absolute;right: 0;bottom: 0;
	}
</style>
